﻿using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MiniStock.BusinessLogic.EventQueue.Data;
using MiniStock.BusinessLogic.EventQueue.Queue;
using MiniStock.Entities.Common;
using MiniStock.Entities.Common.Events;
using MiniStock.Entities.Stock.Events;
using MiniStock.Entities.Stock.Events.Customer;

namespace MiniStock.BusinessLogic.EventDispatcher.Test
{
    
    
    /// <summary>
    ///This is a test class for EventListenerLogicTest and is intended
    ///to contain all EventListenerLogicTest Unit Tests
    ///</summary>
    [TestClass()]
    public class EventListenerLogicTest
    {
        public static IUnityContainer RootContainer;


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            SerializationHelper.KnownEventTypes.Clear();
            SerializationHelper.RegisterKnownEventTypes(new[] { typeof(CustomerEventBase) });
            EventBaseExtender.InitSerializer();

            RootContainer = new UnityContainer();
            RootContainer.RegisterType<IEventProcessorRepository, EventProcessorDbRepository>();
        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
            using (var db = new EventDispatcherContext())
            {
                db.Database.ExecuteSqlCommand("delete from EventListeners");
            }
        }
        //
        #endregion

        private void RegisterListeners(EventProcessorCommunicationLogic sut)
        {
            sut.RegisterEventProcessor("demo1");
            sut.RegisterEventProcessor("demo2");
        }


        /// <summary>
        ///A test for GetNextClientData
        ///</summary>
        //[TestMethod()]
        public void GetNextClientDataTest()
        {
            var sut = RootContainer.Resolve<EventProcessorCommunicationLogic>();
            RegisterListeners(sut);

            var result = sut.GetNextClientData();
            Assert.IsNotNull(result);
            Assert.AreEqual("demo1", result.EndpointAddress);

            result = sut.GetNextClientData();
            Assert.IsNotNull(result);
            Assert.AreEqual("demo2", result.EndpointAddress);

            result = sut.GetNextClientData();
            Assert.IsNotNull(result);
            Assert.AreEqual("demo1", result.EndpointAddress);
        }

        /// <summary>
        ///A test for UnregisterEventListener
        ///</summary>
        //[TestMethod()]
        [DeploymentItem("MiniStock.BusinessLogic.EventDispatcher.dll")]
        public void UnregisterEventListenerTest()
        {
            var sut = RootContainer.Resolve<EventProcessorCommunicationLogic>();
            RegisterListeners(sut);

            var result = sut.GetNextClientData();
            Assert.IsNotNull(result);
            Assert.AreEqual("demo1", result.EndpointAddress);

            result = sut.GetNextClientData();
            Assert.IsNotNull(result);
            Assert.AreEqual("demo2", result.EndpointAddress);

            result = sut.GetNextClientData();
            Assert.IsNotNull(result);
            Assert.AreEqual("demo1", result.EndpointAddress);

            sut.UnregisterEventProcessor("demo2");

            result = sut.GetNextClientData();
            Assert.IsNotNull(result);
            Assert.AreEqual("demo1", result.EndpointAddress);
        }
    }
}
